package com.example.njut;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.Prediction;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.Toast;

import java.util.ArrayList;

public class CheckActivity extends AppCompatActivity {

    GestureOverlayView gView;
    GestureLibrary gestureLibrary;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check);

        //读取程序创建的手势库
        gestureLibrary = GestureLibraries.fromFile("/mnt/sdcard/zhanghan");
        if(gestureLibrary.load()){
            Toast.makeText(CheckActivity.this,"手势文件加载成功!",Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(CheckActivity.this,"手势文件加载失败!",Toast.LENGTH_SHORT).show();
        }
        gView = (GestureOverlayView)findViewById(R.id.gView);
        gView.setGestureColor(Color.BLACK);
        gView.setGestureStrokeWidth(30);
        gView.addOnGesturePerformedListener(new GestureOverlayView.OnGesturePerformedListener() {
            @Override
            public void onGesturePerformed(GestureOverlayView gestureOverlayView, final Gesture gesture) {
                ArrayList<Prediction> predictions = gestureLibrary.recognize(gesture);
                ArrayList<String> result = new ArrayList<String>();
                for(Prediction pred : predictions){
                    if(pred.score>2.0){
                        result.add("与手势【"+pred.name+"】相似");
                    }
                }
                if(result.size()>0){
                    Intent intent = new Intent();
                    intent.setClass(CheckActivity.this,GameActivity.class);
                    startActivity(intent);
                    CheckActivity.this.finish();
                    Toast.makeText(CheckActivity.this,"手势验证成功",Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(CheckActivity.this,"手势验证失败",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}
